package kuzmenkov.oip.adapters.sun.jai;

import kuzmenkov.oip.adapters.ImageProcessingAdapter;
import kuzmenkov.oip.common.FileTool;

import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;
import java.awt.image.renderable.ParameterBlock;
import java.net.URL;
import java.util.logging.Logger;

/**
 * User: Michael Kuzmenkov
 * Date: 26.03.2009
 * Time: 1:32:53
 */
public class AddingTwoImagesAdapter extends  ImageProcessingAdapter {
    private static final Logger log = Logger.getLogger(AddingTwoImagesAdapter.class.getName());


    /**
     * It takes two parameters. The parameters are paths to source images.
     * It returns one result. This is the path to the output image (java.lang.String)
     *
     * @param params the source images
     * @return the path to the output image
     */
    public Object[] process(Object[] params) throws Exception {
        // Load the source image from a file.
        String path1 = (String) params[0];
        String path2 = (String) params[1];
        log.info("path1=" + path1 + ", path2=" + path2);

        FileTool ftool = FileTool.getInstance();
        path1 = ftool.relPathToAbs(path1);
        path2 = ftool.relPathToAbs(path2);

        RenderedOp srcImage1 = JAI.create("fileload", path1);
        RenderedOp srcImage2 = JAI.create("fileload", path2);


        ParameterBlock block = new ParameterBlock();
        block.addSource(srcImage1);
        block.addSource(srcImage2);
        RenderedOp dstImage = JAI.create("add", block, null);

        
        String dstFileName = "1.png";
        String dstPath = ftool.getTempDir(getSessionId())+dstFileName;
        JAI.create("filestore", dstImage, dstPath, "PNG", null);

        String relDstPath = ftool.getRelativeTempDir(getSessionId())+dstFileName;
        return new Object[]{relDstPath};
    }
}